package com.mida.dms.knw.vo;

import io.swagger.annotations.ApiModelProperty;
import org.apache.commons.lang3.builder.ToStringBuilder;


/**
 * 分页类
 * @since JDK1.7
 * */
public class Page implements java.io.Serializable{

    private static final long serialVersionUID = 1L;
    //前一页
    private Boolean hasPrePage;
    //后一页
    private Boolean hasNextPage;
    //每页显示多少条:默认10条
    @ApiModelProperty(value = "每页显示多少条", required = false)
    private Long everyPage = 10L;
    //总页数
    private Long totalPage;
    //当前第多少页:默认第1页
    @ApiModelProperty(value = "当前页", required = false)
    private Long currentPage;
    //开始下标
    private Long beginIndex;
    //结束下标
    private Long endinIndex;
    //总共多少条
    @ApiModelProperty(value = "总数", required = false)
    private Integer totalCount;

    public Page() {
    }

    /**
     * 常用,用于计算分页
     * */
    public Page(Integer totalRecords){
        this.totalCount = totalRecords;
        setTotalPage(getTotalPage(totalRecords));
    }

    /**
     * 设置每页显示多少条时使用
     * */
    public Page(Long everyPage, Integer totalRecords){
        this.everyPage = everyPage;
        this.totalCount = totalRecords;
        setTotalPage(getTotalPage(totalRecords));
    }

	public Page(Long pageNo, Long pageSize) {
		this.everyPage = pageSize;
		this.currentPage = pageNo;
	}


    /**
     * 计算总页数
     * */
    private Long getTotalPage(Integer totalRecords) {
        Long totalPage = 0L;
        everyPage = everyPage == null ? 10L : everyPage;
        if (totalRecords % everyPage == 0)
            totalPage = totalRecords / everyPage;
        else {
            totalPage = totalRecords / everyPage + 1;
        }
        return totalPage;
    }


    public Long getBeginIndex() {
//        if (currentPage==1){
//            this.beginIndex=currentPage;
//        }else{

            this.beginIndex = (currentPage-1) * everyPage+1;
//        }
        return this.beginIndex;
    }

    public void setBeginIndex(Long beginIndex) {
        this.beginIndex = beginIndex;
    }

    public Long getCurrentPage() {
        //this.currentPage = currentPage == 0 ? 1 : currentPage;
        return this.currentPage;
    }

    public void setCurrentPage(Long currentPage) {
        if(0 == currentPage){
            //currentPage = 1L;
        }
        this.currentPage = currentPage;
    }

    public Long getEveryPage() {
        this.everyPage = everyPage == 0 ? 10 : everyPage;
        return this.everyPage;
    }

    public void setEveryPage(Long everyPage) {
        this.everyPage = everyPage;
    }

    public Boolean getHasNextPage() {
        this.hasNextPage = (currentPage != totalPage) && (totalPage != 0);
        return this.hasNextPage;
    }

    public void setHasNextPage(Boolean hasNextPage) {
        this.hasNextPage = hasNextPage;
    }

    public Boolean getHasPrePage() {
        this.hasPrePage = currentPage != 1;
        return this.hasPrePage;
    }

    public void setHasPrePage(Boolean hasPrePage) {
        this.hasPrePage = hasPrePage;
    }

    public Long getTotalPage() {
        return this.totalPage;
    }

    public void setTotalPage(Long totalPage) {
    //    if(totalPage != 0 && this.currentPage >= totalPage){
            //this.currentPage = totalPage-1;
    //        this.currentPage = totalPage;
    //    }
        this.totalPage = totalPage;
    }

    public Integer getTotalCount() {
        return this.totalCount;
    }

    public void setTotalCount(Integer totalCount) {
        setTotalPage(getTotalPage(totalCount));
        this.totalCount = totalCount;
    }

    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this);
    }

}
